#import "NSImage+SMRAdditions.h"
#import <QuickLook/QuickLook.h>


@implementation NSImage (SMRAdditions)

+(NSImage *)thumbnailImageFromURL:(NSURL *)aURL withMaximumSize:(NSSize)maxSize asIcon:(BOOL)displayAsIcon {
	CGSize thumbnailSize = CGSizeMake(maxSize.width, maxSize.height);
	CFBooleanRef isIcon = displayAsIcon ? kCFBooleanTrue : kCFBooleanFalse;
	NSDictionary *optionsDictionary = [NSDictionary dictionaryWithObject:(id)isIcon forKey:(id)kQLThumbnailOptionIconModeKey];
	CGImageRef quickLookImageRef = QLThumbnailImageCreate(kCFAllocatorDefault, (CFURLRef)aURL, thumbnailSize, (CFDictionaryRef)optionsDictionary);
	if (quickLookImageRef == NULL) 
		return nil;
	NSSize imageSize = NSMakeSize(CGImageGetWidth(quickLookImageRef), CGImageGetHeight(quickLookImageRef));
	NSImage* quickLookImage = [[NSImage alloc] initWithCGImage:quickLookImageRef size:imageSize];
	CFRelease(quickLookImageRef);
	return [quickLookImage autorelease];
}

@end
